﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using NTTDATA.BEENChina.ResultCertification.Config;
using NTTDATA.BEENChina.ResultCertification.PropertyGridUtil;

namespace NTTDATA.BEENChina.ResultCertification.Checker.Numeric
{
   
    [Serializable]
    [VerfityAttribute(Id = "VerfityD0009", Name = "连续的N组数值间有空内容时抽出", CategoryType = EnumCategoryType.数值, Model = true)]
    public class VerfityD0009 : VerfityOneResult
    {

       
        string _emptyRowes = string.Empty;

        [Category("设定检查的空白数值行"), Description("要检查的空白数值行")]
        [EditorAttribute(typeof(PropertyGridRichText), typeof(System.Drawing.Design.UITypeEditor))]
        public String EmptyRows
        {
            get { return _emptyRowes; }
            set { _emptyRowes = value; }
        }



        [Category("设定检查的连续行数"), Description("要检查的连续行数")]
        public int SeriateRows
        {
            get; set;
        }

        private List<int> GetEmptyRows()
        {
            
               
                var rows = _emptyRowes.Split(new char[]{','});

                var emptyRows = new List<int>();
                foreach (string row in rows)
                {
                    int r;

                    if (!int.TryParse(row, out r))
                    {
                        r = -1;
                    }

                    emptyRows.Add(r);
                }

                return emptyRows;
            
        }


        public override bool CheckResult(List<List<string>> result, string entryCommon)
        {
            bool bresult = false;
            if (result == null || entryCommon == "30")
            {
                return false;
            }


            if (result.Count < SeriateRows - 1)
            {
                return false;
            }

            for (int i = 0; i < result.Count; i++)
            {
 
                List<List<string>> checkResult = null;
                
                for (int j = 0; j < SeriateRows; j++)
                {                   
                    if (i + j < result.Count)
                    {
                        if (checkResult == null) 
                            checkResult  = new List<List<string>>();
                        
                        checkResult.Add( result[i + j]);
                    }                   
                }


                if (checkResult != null && checkResult.Count == SeriateRows)
                {
                    bresult =  CheckEmpty(checkResult);
                }

                if (bresult)
                {
                    break;
                }
            }

           

            if ( bresult)
            {
                System.Console.WriteLine("VerfityD0009");
            }

            return bresult;
        }




        private bool CheckEmpty(List<List<string>> result)
        {
            bool bresult = false;

            var emptyRows = GetEmptyRows();

            for (int i = 0; i < result.Count; i++)
            {
                //非空白行有数据
                if (!emptyRows.Contains(i))
                {
                    bresult = result[i].Any(colStr =>
                    {
                        string numericRsult = GetSpeicficResult(colStr); 
                        return  string.IsNullOrEmpty(numericRsult);
                    });
                }

                if (bresult)
                    break;
            }

            if (bresult)
                return false;

            for (int i = 0; i < result.Count; i++)
            {
                //空白行没有数据
                if ( emptyRows.Contains(i))
                {
                    bresult = result[i].Any(colStr =>
                    {
                        string numericRsult = GetSpeicficResult(colStr);
                        return  string.IsNullOrEmpty(numericRsult);
                    });
                }

                if (bresult)
                    break;
            }

            return bresult;

            /*  //检验条件 空白行为空白行
            //非空白行 为非空白行
            for (int i = 0; i < result.Count; i++)
            {
                if (emptyRows.Contains(i))
                {
                    
                   bresult =   result[i].All(colStr =>
                                {
                                    bool bcheck  =false;
                                    string numericRsult = GetSpeicficResult(colStr);
                                    bcheck = string.IsNullOrEmpty(numericRsult);

                                    return bcheck;
                                });
                    
                }
                else
                {
                    bresult =  result[i].Any(colStr =>
                              {
                                bool bcheck  =false;
                                string numericRsult = GetSpeicficResult(colStr);
                                bcheck = !string.IsNullOrEmpty(numericRsult);

                                return bcheck;
                                                        
                              });
                    
                }

                if (!bresult)
                    break;
            }*/

            return bresult;
        }

        private string GetSpeicficResult(string colResult)
        {
            var splitResult = new Split.SplitEntryResult(ChildImageObject.CategorySetting.DataFormat, colResult);
            return splitResult.GetNumeric();
        }
    }
}
